package demo.thundersoft.cn.photoeditor;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.IOException;

/**
 * Author：雨沐风  2016-08-13 18:39
 * Email：admin@yumufeng.com
 */
public class EditImageActivity extends Activity implements View.OnClickListener {

    private Button leftButton;
    private Button rightButton;
    private ImageView imageDemo;
    private TextView imageSubmit;
    private Bitmap pictrueBitmap;

    @TargetApi(Build.VERSION_CODES.KITKAT)
    @Override
    protected void onCreate(Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_editimage);
        leftButton = (Button) findViewById(R.id.bt_editImage_left);
        rightButton = (Button) findViewById(R.id.bt_editImage_right);
        imageDemo = (ImageView) findViewById(R.id.iv_editImage_demo);
        imageSubmit = (TextView) findViewById(R.id.tv_editimage_submit);
        Uri imageUri = getIntent().getData();
        try {
            pictrueBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
        } catch (IOException e) {
            e.printStackTrace();
        }
        findViewById(R.id.iv_editImage_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        setBitmapToDemo(null);
        leftButton.setOnClickListener(this);
        rightButton.setOnClickListener(this);
        imageSubmit.setOnClickListener(this);

    }

    private void setBitmapToDemo(Bitmap bm) {
        if (bm == null) {
            imageDemo.setImageBitmap(pictrueBitmap);
        }else {
            imageDemo.setImageBitmap(bm);
        }
    }

    /**
     * 图片旋转
     * @param tmpBitmap
     * @param degrees
     * @return
     */
    public static Bitmap rotateToDegrees(Bitmap tmpBitmap, float degrees) {
        Matrix matrix = new Matrix();
        matrix.reset();
        matrix.postRotate(degrees);

        Bitmap bm1 = Bitmap.createBitmap(tmpBitmap.getWidth(), tmpBitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bm1);
        Bitmap bm2 = Bitmap.createBitmap(tmpBitmap, 0, 0, tmpBitmap.getWidth(), tmpBitmap.getHeight(), matrix,
                true);
        canvas.drawBitmap(bm2, new Rect(0, 0, bm2.getWidth(), bm2.getHeight()),new Rect(0, 0, tmpBitmap.getWidth(), tmpBitmap.getHeight()), null);
        return bm1;
    }

    /**
     * 读取照片exif信息中的旋转角度
     * @param path 照片路径
     * @return角度
     */
    public static int readPictureDegree(String path) {
        int degree = 0;
        try {
            ExifInterface exifInterface = new ExifInterface(path);
            int orientation =
                    exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                            ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return degree;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_editImage_left:
                pictrueBitmap = rotateToDegrees(pictrueBitmap, -90);
                setBitmapToDemo(pictrueBitmap);
                break;
            case R.id.bt_editImage_right:
                pictrueBitmap = rotateToDegrees(pictrueBitmap, 90);
                setBitmapToDemo(pictrueBitmap);
                break;
            case  R.id.tv_editimage_submit:
                Intent data = new Intent();
                data.setData(Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(),pictrueBitmap,null,null)));
                setResult(RESULT_OK,data);
                finish();
                break;
        }
    }
}
